/**
 * @class WebOs.Kernel.CoreBase
 * @author Hu Xiaoguang
 * @markdown Kernel核心管理 serviceMgr初始化，init中所有Services初始化 viewFrameWorks初始化或者启动
 *           FileSystem初始化
 */
Ext.define('WebOs.Kernel.CoreBase', {
			version : 1.0,

			idgen : null,

			sfs : null,
			smgr : null,
			vmgr : null,
			umgr : null,
			pmgr : null,

			isReady : false,
			startLevel : 1,

			mixins : {
				observable : 'Ext.util.Observable'
			},

			requires : ['WebOs.Kernel.model.ServiceBase',
					'WebOs.Kernel.model.UIServiceBase',
					'WebOs.Kernel.model.FileModel',
					'WebOs.Kernel.model.UserModel',
					'WebOs.Kernel.ServiceFileSystem',
					'WebOs.Kernel.ServiceMgr', 'WebOs.Kernel.ViewFrameMgr',
					'WebOs.Kernel.UserMgr', 'WebOs.Kernel.PermissionMgr'],

			constructor : function(config) {
				var me = this;
				me.addEvents('ready');
				me.idgen = new Ext.data.SequentialIdGenerator({
							id : 'webos-kernel',
							prefix : 'webos-kernel-'
						});

				me.sfs = new WebOs.Kernel.ServiceFileSystem({
							kernel : me
						});
				me.smgr = new WebOs.Kernel.ServiceMgr({
							kernel : me
						});

				me.vmgr = new WebOs.Kernel.ViewFrameMgr({
							kernel : me
						});

				me.umgr = new WebOs.Kernel.UserMgr({
							kernel : me
						});

				me.pmgr = new WebOs.Kernel.PermissionMgr({
							kernel : me
						});

				me.startLevel = webosCfg.startLevel || 1;

				if (Ext.isReady) {
					Ext.Function.defer(me.init, 10, me);
				} else {
					Ext.onReady(me.init, me);
				}
			},

			init : function() {
				var me = this;
				me.initKernelService();
				// if(me.startLevel >1){
				// me.vmgr.initVFrame();
				// }
				// me.initUserService();
				me.isReady = true;
				me.fireEvent('ready', me);
			},

			mountUserService : function(services) {
				var me = this;
				// 挂载user目录结构
				var utoc = {
					pname : Constants.SFS_ROOT_NAME,
					name : Constants.SFS_USER_NAME,
					text : Constants.SFS_USER_NAME,
					serviceType : Constants.SFS_TYPE_TOC
				};
				me.sfs.mountFilesToParentFile(utoc,
						Constants.SFS_ROOT_NAME,
						Constants.START_LEVEL_USER);
				// 挂载用户初始化服务文件
				if (!services) {
					return;
				}
				var userInitService = services;
				userInitService.each(function(item) {
							if (item.get('serviceType') == 2
									&& !item.get('serviceCls')) {
								log('[WARN]',
										'user service must to have serviceCls attr');
								return;
							}
							var us = {};
							us.name = item.get('name');
							us.text = item.get('text')
									? item.get('text')
									: item.get('name');
							us.serviceType = item.get('serviceType');
							us.serviceCls = item.get('serviceCls');
							us.pname = item.get('pname');
							me.sfs.mountFilesToParentFile(us,
									Constants.SFS_USER_NAME,
									Constants.START_LEVEL_USER);
						});

//				if (userInitService && userInitService.length > 0) {
//					for (var p in userInitService) {
//
//						var us = userInitService[p];
//						if (!us.serviceCls) {
//
//						}
//						if (!us.name)
//							us.name = me.idgen.generate();
//						if (!us.text)
//							us.text = us.name;
//						me.sfs.mountFilesToParentFile(us,
//								webOsConstants.SFS_USER_NAME,
//								webOsConstants.START_LEVEL_USER);
//						// if(me.startLevel == 3 && us.autoStart){
//						// try{
//						// me.smgr.startServiceByKey(us.name);
//						// }catch(e){
//						// log('[ERROR] start user service error',e);
//						// }
//						// }
//					}
//				}
			},

			initKernelService : function() {
				var me = this;
				// 挂载kernel目录结构
				var ktoc = {
					pname : Constants.SFS_ROOT_NAME,
					name : Constants.SFS_KERNEL_NAME,
					text : Constants.SFS_KERNEL_NAME,
					serviceType : Constants.SFS_TYPE_TOC
				};
				me.sfs.mountFilesToParentFile(ktoc,
						Constants.SFS_ROOT_NAME,
						Constants.START_LEVEL_INIT);

				// 初始化并挂载kernel默认服务文件 ，而后直接启动
				var kernelInitService = webosCfg.kernelser;
				if (kernelInitService && kernelInitService.length > 0) {
					for (var p in kernelInitService) {
						var ks = kernelInitService[p];
						if (!ks.serviceCls) {
							console
									.log('[WARN]',
											'init service must to have serviceCls attr');
							continue;
						}
						if (!ks.name)
							ks.name = me.idgen.generate();
						if (!ks.text)
							ks.text = ks.name;
						me.sfs.mountFilesToParentFile(ks,
								Constants.SFS_KERNEL_NAME,
								Constants.START_LEVEL_INIT);
						try {
							me.smgr.startServiceByKey(ks.name);
						} catch (e) {
							console
									.log('[ERROR] start kernel service error',
											e);
						}
					}
				}
			},

			onReady : function(fn, scope) {
				if (this.isReady) {
					fn.call(scope, this);
				} else {
					this.on({
								ready : fn,
								scope : scope,
								single : true
							});
				}
			}
		});